import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.lang.reflect.Method;

public class StudentFileDriver {

	public static void main(String[] args) {
		List<Student> allStudents = new ArrayList<>();

		allStudents.add(new Student("Fred", "Flintstone", 23));
		allStudents.add(new Student("Barney", "Rubble", 44));
		allStudents.add(new Student("Wilma", "Flintstone", 55));

		String fileName = "myFile.txt";

		Path file = Paths.get(fileName);

		writeAllStudentsToFile(file, allStudents);

		List<Student> copyOfStudents = readAllStudentsFromFile(file);

		for (Student who : copyOfStudents) {
			System.out.println(who.getFirstName());
		}

		String objectFileName = "myFile.dat";

		writeObjectStudentFile(objectFileName, allStudents);

		List<Student> copyStudents = readObjectStudentFile(objectFileName);

		System.out.println("Copy of Students");

		for (Student who : copyStudents) {
			System.out.println(who.getFirstName());
		}

		String xmlFileName = "myFile.xml";

		writeXMLStudentFile(xmlFileName, allStudents);

		System.out.println(" XML Copy of Students");

		List<Student> anotherListStudents = readXMLStudentFile(xmlFileName);

		for (Student who : anotherListStudents) {
			System.out.println(who.getFirstName());
		}

		printMethods();

	}

	private static void writeObjectStudentFile(String fileName,
			List<Student> allStudents) {

		try (ObjectOutputStream writer = new ObjectOutputStream(
				new FileOutputStream(fileName))) {

			writer.writeObject(allStudents);

		} catch (IOException x) {
			System.err.format("IOException: %s%n", x);
		}
	}

	private static List<Student> readObjectStudentFile(String fileName) {
		List<Student> copyStudents = null;

		try (ObjectInputStream reader = new ObjectInputStream(
				new FileInputStream(fileName))) {

			copyStudents = (List<Student>) reader.readObject();

		} catch (IOException | ClassNotFoundException x) {
			System.err.format("IOException: %s%n", x);
		}

		return copyStudents;
	}

	private static void writeXMLStudentFile(String fileName,
			List<Student> allStudents) {
		try (XMLEncoder writer = new XMLEncoder(new FileOutputStream(fileName))) {
			writer.writeObject(allStudents);
		} catch (IOException x) {
			System.err.format("IOException: %s%n", x);
		}
	}

	private static List<Student> readXMLStudentFile(String fileName) {
		List<Student> anotherListStudents = null;
		try (XMLDecoder reader = new XMLDecoder(new FileInputStream(fileName))) {
			anotherListStudents = (List<Student>) reader.readObject();
		} catch (IOException x) {
			System.err.format("IOException: %s%n", x);
		}

		return anotherListStudents;
	}

	private static void printMethods() {
		System.out.println("Methods of Students");

		Class<Student> aClass = Student.class;

		for (Method aMethod : aClass.getDeclaredMethods()) {
			System.out.println(aMethod.getName());
		}
	}

	private static List<Student> readAllStudentsFromFile(Path file) {
		List<Student> bunchOStudent = new ArrayList<>();

		Charset charset = Charset.forName("US-ASCII");

		try (BufferedReader reader = Files.newBufferedReader(file, charset)) {

			String aLine = "";
			while ((aLine = reader.readLine()) != null) {
				System.out.println(aLine);
				Scanner scan = new Scanner(aLine);

				String firstName = scan.next();
				String lastName = scan.next();
				int age = scan.nextInt();

				Student aStudent = new Student(firstName, lastName, age);
				bunchOStudent.add(aStudent);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bunchOStudent;
	}

	private static void writeAllStudentsToFile(Path file,
			List<Student> allStudents) {
		Charset charset = Charset.forName("US-ASCII");

		try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {

			for (Student who : allStudents) {
				writeOneStudent(who, writer);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void writeOneStudent(Student who, BufferedWriter writer)
			throws IOException {
		String first = who.getFirstName();
		String last = who.getLastName();
		String age = who.getAge() + "";

		int currentAge = who.getAge();
		Integer currentAgeInteger = new Integer(currentAge);
		age = currentAgeInteger.toString();

		String result = first + " " + last + " " + age;

		writer.write(result);
		writer.newLine();
	}

}
